package com.feng.springboothelloworld.controller;

import com.feng.springboothelloworld.dao.DepartmentDao;
import com.feng.springboothelloworld.dao.EmployeeDao;
import com.feng.springboothelloworld.domain.Department;
import com.feng.springboothelloworld.domain.Employee;
import org.apache.tomcat.util.security.MD5Encoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Collection;

@Controller
@RequestMapping("employee")
public class EmployeeControlle {
    @Autowired
    EmployeeDao employeeDao;
    @Autowired
    DepartmentDao departmentDao;

    @RequestMapping("getAll")
    public String getAll(Model model){
        Collection<Employee>employees=employeeDao.getAll();
        model.addAttribute("emps",employees);
        return "emp/list";
    }
    @GetMapping("add")
    public String add(Model model){
        Collection<Department>departments=departmentDao.getAll();
        model.addAttribute("departments",departments);
        return "emp/add";
    }
    @PostMapping("addover")
    public String addover(Employee employee){
        System.out.println("----"+employee);
        employeeDao.save(employee);
        return "redirect:/employee/getAll";
    }
    @RequestMapping("edit/{id}")
    public String edit(@PathVariable Integer id, Model model){
        System.out.println(id);
        Employee employee=employeeDao.getByid(id);
        model.addAttribute("emp",employee);
        Collection<Department>departments=departmentDao.getAll();
        model.addAttribute("departments",departments);
        return "emp/update";
    }
    @RequestMapping("update")
    public String update(Employee employee){
        employeeDao.save(employee);
        return "redirect:/employee/getAll";
    }
    @RequestMapping("delete/{id}")
    public String delete(@PathVariable Integer id){
        System.out.println(id);
        employeeDao.delete(id);
        return "redirect:/employee/getAll";
    }
}
